package com.app.miwawa.api.esm.service.bo;

import javax.annotation.Resource;

import org.framework.common.api.entity.CommonEntityAware;
import org.framework.common.api.query.Criterion.Operator;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.service.dao.CommonDaoAware;
import org.framework.common.api.support.constant.SystemConstants;
import org.framework.common.api.support.model.ProcessModel;
import org.framework.common.api.support.model.QueryModel;
import org.framework.common.ext.util.ClassUtil;
import org.framework.common.ext.util.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.eam.service.dao.ClassesStudentDao;
import com.app.miwawa.api.esm.entity.contract.Contract;
import com.app.miwawa.api.esm.entity.contract.Refund;
import com.app.miwawa.api.esm.entity.customer.Customer;
import com.app.miwawa.api.esm.entity.customer.Student;
import com.app.miwawa.api.esm.entity.view.ContractView;
import com.app.miwawa.api.esm.service.dao.ContractDao;
import com.app.miwawa.api.esm.service.dao.CustomerDao;

/**
 * 业务接口
 * <p>描述: 合同－业务接口</p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date 2016年1月23日 上午9:37:10
 * @version 1.0.2016
 */
@Service("contractBo")
public class ContractBo extends CommonBoAdapter<Contract, String> {
	
	@Autowired
	private ContractDao contractDao;
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	private ClassesStudentDao csdao;
	
	@Resource(name = "defaultDao")
	private CommonDaoAware<CommonEntityAware<String>, String> defaultDao;


	/**
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#insert(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年1月23日 上午9:44:41
	 */
	@Override
	public Contract insert(Contract entity) throws Exception {
		return contractDao.save(entity);
	}
	
	/**
	 * 停课会员还原
	 * @see org.framework.common.api.service.bo.CommonBoAdapter#update(org.framework.common.api.entity.CommonEntityAware)
	 * @author chanlong(陈龙)
	 * @date 2016年1月30日 下午11:39:23
	 */
	@Override
	public Contract update(Contract entity) throws Exception {
		Contract bean = contractDao.findOne(entity.getId());
		
		// 停课恢复
		if("pause".equals(bean.getStatus())){
			ClassUtil.copyProperties(entity, bean);
			return contractDao.saveAndFlush(bean);
		}else{
			// 查询正常上课的学员
			QueryModel query = new QueryModel(ContractView.class);
			query.addAndQuery("item", bean.getItem(), Operator.EQ);
			query.addAndQuery("rested", "0", Operator.GT);
			query.addAndQuery("status", new String[]{"0","pause"}, Operator.IN);
			query.addAndQuery("studentid", bean.getStudent(), Operator.EQ);
			
			if(defaultDao.count(query) > 0){
				return null;
			}else{
				ClassUtil.copyProperties(entity, bean);
				return contractDao.saveAndFlush(bean);
			}
		}
	}
	
	/**
	 * 更新.
	 * @param ids
	 * @author chanlong(陈龙)
	 * @param entity 
	 * @throws Exception 
	 * @date 2016年2月8日 下午2:51:12
	 */
	public Integer update(final String[] ids, final Customer entity) throws Exception {
		return contractDao.update(entity.getUserid(), entity.getUsername(), ids);
	}
	
	/**
	 * 退费及终止.
	 * @param entity
	 * @return
	 * @author chanlong(陈龙)
	 * @throws Exception 
	 * @date 2016年5月1日 下午10:04:29
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Refund refund(final Refund entity) throws Exception {
		Refund refund = (Refund)defaultDao.insert(entity);
		
		String studentid = refund.getStudentId();
		String contractid = refund.getContractId();
		String customerid = refund.getCustomerId();
		
		ProcessModel process = ProcessModel.factory();
		
		if("退费".equals(refund.getType())){
			// 逻辑删除分班信息
			csdao.remove(studentid);
			
			// 更新合同状态
			contractDao.update("refund", contractid);
			
			// 查询指定客户名下执行中的合同
			QueryModel query = new QueryModel(ContractView.class);
			query.addAndQuery("rested", "0", Operator.GT);
			query.addAndQuery("status", "0", Operator.EQ);
			query.addAndQuery("customerid", customerid, Operator.EQ);
			
			// 逻辑删除客户信息
			if(defaultDao.count(query) <= 0){
				customerDao.delete(SystemConstants.DICT_DELETE_VALID, customerid);
			}
			
			// 逻辑删除学员信息
			Student student = (Student)defaultDao.loader((Class)Student.class, studentid);
			student.setInvalid(SystemConstants.DICT_DELETE_VALID);
			defaultDao.update(entity);
			
			// 删除请假上限
			process.jdbc.update(process.properties.getProperty("contract.delete.leavelimit"), contractid);
			
		}else{
			// 查询是否存在未启用合同
			int count = process.jdbc.queryForObject(process.properties.getProperty("contract.close.has"), Integer.class, studentid);
			if(count == 0){
				// 逻辑删除分班信息和请假上限
				process.jdbc.update(process.properties.getProperty("contract.remove.student"),contractid);
			}
			// 更新合同状态
			contractDao.update("finish", contractid);
		}
		return refund;
	}
	
	/**
	 * 查询最大编码.
	 * @return
	 * @author chanlong(陈龙)
	 * @throws Exception 
	 * @date 2016年1月23日 下午5:36:44
	 */
	public String maxcode() throws Exception{
		String code = contractDao.maxcode();
		return SystemUtils.maxcode(Contract.CODE_PREFIX_CONTRACT, code, Contract.DEFAULT_CODE_SERIAL);
	}
}
